﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessComponents.DomainModels;
using System.Data.Objects;
using System.Data.Common;
using BusinessComponents.DAL;

namespace BusinessComponents.Managers
{
    public class ReportMgr : IReportMgr
    {
        public IEnumerable<Report1001> fetchRpt1001()
        {

            IEnumerable<Report1001> list = DaoFactory.ReportDao.fetchRpt1001(5);

            return list;
        }

        public IEnumerable<Report1002> fetchRpt1002(DateTime dateFrom, DateTime dateTo)
        {
            TimeSpan ts = dateTo - dateFrom;
            int totalDays = ts.Days;

            WaterFrontEntities wfe = new WaterFrontEntities();
            var records = from r in wfe.ReservationDetails
                          where r.DateReserved >= dateFrom &
                            r.DateReserved <= dateTo
                          group r by r.RoomNo into g
                          orderby g.Count()
                          select new { RoomNo = g.Key, daysCount = g.Count() };


            List<Report1002> list = new List<Report1002>();

            foreach (var r in records)
            {
                double rate = (double)r.daysCount / totalDays;

                list.Add(new Report1002(r.RoomNo, rate));
            }

            return list;
        }

        public IEnumerable<Report2001> fetchRpt2001()
        {

            IEnumerable<Report2001> list = DaoFactory.ReportDao.fetchRpt2001();
            double sumDays = 0;
            foreach (var rpt in list)
            {
                sumDays += rpt.TotalDay;
            }

            bool isFirst = true;
            foreach (var rpt in list)
            {
                if (isFirst)
                {
                    rpt.Expanded = true;
                    isFirst = false;
                }
                rpt.Ratio = rpt.TotalDay / sumDays;
            }

            return list;
        }
    }
}
